package com.jiawa.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.wiki.domain.Ebook;
import com.jiawa.wiki.domain.EbookExample;
import com.jiawa.wiki.mapper.EbookMapper;
import com.jiawa.wiki.req.EbookQueryReq;
import com.jiawa.wiki.req.EbookSaveReq;
import com.jiawa.wiki.response.EbookResp;
import com.jiawa.wiki.response.PageResp;
import com.jiawa.wiki.utils.CopyUtil;
import com.jiawa.wiki.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EbookService {

    @Resource
    private EbookMapper ebookMapper;

    @Resource
    private SnowFlake snowFlake;

    private final static Logger LOG = LoggerFactory.getLogger(EbookService.class);

    /**
     * name模糊查询
     * 支持分页查询：注意 PageHelper 只对一条查询sql有效，如果配置下有多个sql,应该写在要查询的sql上即可
     * @param req
     * @return
     */
    public PageResp<EbookResp> list(EbookQueryReq req) {
        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        // 动态sql判断有没有name
        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }
        // 判断是不是分类查询
        if (!ObjectUtils.isEmpty(req.getCategory2Id())) {
            // 根据分类id查询
            criteria.andCategory2IdEqualTo(req.getCategory2Id());
        }
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);

        PageInfo<Ebook> pageInfo = new PageInfo<>(ebookList);
        LOG.info("总页数：{}", pageInfo.getPages());
        LOG.info("总行数：{}", pageInfo.getTotal());
        // 持久层的需要传化为 List<EbookResp>
        /* List<EbookResp> respList = new ArrayList<>();
         for (Ebook ebook : ebookList) {
            EbookResp ebookResp = new EbookResp();
            //BeanUtils.copyProperties(ebook, ebookResp);
            // 单体复制
            EbookResp copy = CopyUtil.copy(ebook, EbookResp.getClass());
            respList.add(copy);
        } */

        // 列表复制 上述代码简化
        List<EbookResp> respList = CopyUtil.copyList(ebookList, EbookResp.class);

        PageResp<EbookResp> pageResp = new PageResp<>();
        pageResp.setList(respList);
        pageResp.setTotal(pageInfo.getTotal());
        LOG.info("{}", respList);
        return pageResp;
    }

    /**
     * 保存电子书（看请求参数id是否为空） 修改电子书
     * @param req
     */
    public void save(EbookSaveReq req) {
        Ebook ebook = CopyUtil.copy(req, Ebook.class);
        if (ObjectUtils.isEmpty(ebook.getId())) {
            // 新增 设置id 此ID是雪花算法实现 不是uuid 和自增ID
            ebook.setId(snowFlake.nextId());
            ebookMapper.insert(ebook);
        } else {
            // 修改
            ebookMapper.updateByPrimaryKey(ebook);
        }
    }

    public void delete(Long id) {
        ebookMapper.deleteByPrimaryKey(id);
    }
}
